package cn.school.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.school.entity.Grade;
import cn.school.entity.Student;
import cn.school.service.GradeService;
import cn.school.service.StudentService;

/**
 * Servlet implementation class StudentUpdateServlet
 */
@WebServlet("/studentUpdateServlet")
public class StudentUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StudentUpdateServlet() {
		super();
	}

	StudentService service = new StudentService();
	GradeService g = new GradeService();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
//		//
//		if (request.getSession().getAttribute("USER_LOGIN") == null) {
//			out.print("<script>alert('您还没有登录');history.back();</script>");
//
//			return;
//		}
		//
		long stuno = Long.parseLong(request.getParameter("stuno"));

		Student s = service.get(stuno);
		if (s == null) {
			out.print("<script>alert('学生不存在');history.back();</script>");
			return;
		}

		List<Grade> list2 = g.findALL();
		request.setAttribute("grades", list2);

		request.setAttribute("stu", s);

		request.getRequestDispatcher("/WEB-INF/jsp/updateStudent.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
